//
//  MCAllLevelVipViewController.m
//  TLYL
//
//  Created by MC on 2017/11/22.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCAllLevelVipViewController.h"
#import "MCGetAllVipInfoModel.h"
#import <MJRefresh/MJRefresh.h>
#import "MCMineCellModel.h"
#import "MCAllLevelVipTableViewCell.h"


#define MInAlpha   0.01
#define NAVBAR_CHANGE_POINT 10

@interface MCAllLevelVipViewController ()
<
UITableViewDelegate,
UITableViewDataSource

>
typedef void(^MCVIPCenterViewControllerCompeletion)(BOOL result, NSDictionary *data );

/**tableView*/
@property(nonatomic, strong)UITableView *tableView;
/**sectionMarr*/
@property(nonatomic, strong)NSMutableArray*sectionMarr;
@property(nonatomic, strong)NSMutableArray<MCGetAllVipInfoDataModel *>*dataSource;
/*
 * Api数据模型
 */
@property(nonatomic, strong)MCGetAllVipInfoModel * getAllVipInfoModel;

@property(nonatomic, strong)ExceptionView * exceptionView;


@end

@implementation MCAllLevelVipViewController

#pragma mark - life cycle

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setProperty];
    
    [self createUI];
    
    
    
    [self loadData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBarHidden=NO;
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

#pragma mark==================setProperty======================
-(void)setProperty{

    self.view.backgroundColor=RGB(231, 231, 231);
    _sectionMarr= [[NSMutableArray alloc]init];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBarHidden=NO;
    self.title=@"全部等级";
    _dataSource = [[NSMutableArray alloc]init];
    
}

#pragma mark==================createUI======================
-(void)createUI{

    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    //    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //        [self refreashData];
    //    }];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(0);
        make.left.and.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
    }];
    
    
}

-(void)loadData{
    [self.exceptionView dismiss];
    self.exceptionView = nil;
    [BKIndicationView showInView:self.view];
    //UserID    是    String    当前登录用户 ID
    MCGetAllVipInfoModel * getAllVipInfoModel = [[MCGetAllVipInfoModel alloc]initWithDic:@{}];
    [getAllVipInfoModel refreashDataAndShow];
    self.getAllVipInfoModel = getAllVipInfoModel;
    __weak __typeof__ (self) wself = self;
    getAllVipInfoModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {
        wself.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeRequestFailed];
        ExceptionViewAction *action = [ExceptionViewAction actionWithType:ExceptionCodeTypeRequestFailed handler:^(ExceptionViewAction *action) {
            [wself.exceptionView dismiss];
            wself.exceptionView = nil;
            [wself loadData];
        }];
        [wself.exceptionView addAction:action];
        [wself.exceptionView showInView:wself.view];
        
    };
    
    getAllVipInfoModel.callBackSuccessBlock = ^(id manager) {
        
        [wself setData:manager];
        
    };
    
}

-(void)setData:(NSArray *)arr{
    [_dataSource removeAllObjects];

    for (NSDictionary * dic in arr) {
        MCGetAllVipInfoDataModel * model = [MCGetAllVipInfoDataModel mj_objectWithKeyValues:dic];
        [_dataSource addObject:model];
    }
    

    [self createSectionMarr];
    
}

#pragma mark==================loadData======================
-(void)createSectionMarr{
    
    [_sectionMarr removeAllObjects];
    
    
    for (MCGetAllVipInfoDataModel * info in _dataSource) {
        CellModel* model =[[CellModel alloc]init];
        model.reuseIdentifier = [NSString stringWithFormat:@"%@",NSStringFromClass([MCAllLevelVipTableViewCell class])];
        model.className=NSStringFromClass([MCAllLevelVipTableViewCell class]);
        model.height = [MCAllLevelVipTableViewCell computeHeight:nil];
        model.selectionStyle=UITableViewCellSelectionStyleNone;
        model.accessoryType=UITableViewCellAccessoryNone;
        /*
         * 传递参数
         */
        model.userInfo = info;
        SectionModel *model0=[SectionModel sectionModelWithTitle:@"" cells:@[model]];
        model0.headerhHeight=0.0001;
        model0.footerHeight=10;
        [_sectionMarr addObject:model0];
        
    }
    [_tableView reloadData];
    
}


#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _sectionMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SectionModel *sm = _sectionMarr[section];
    return sm.cells.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    SectionModel *sm = _sectionMarr[section];
    return sm.headerhHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    SectionModel *sm = _sectionMarr[section];
    return sm.footerHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [MCAllLevelVipTableViewCell computeHeight:nil];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SectionModel *sm = _sectionMarr[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
    }
    cell.selectionStyle = cm.selectionStyle;
    
//    __weak __typeof(self)wself = self;
    
    if ([cm.className isEqualToString:NSStringFromClass([MCAllLevelVipTableViewCell class])]) {
        
        MCAllLevelVipTableViewCell *ex_cell=(MCAllLevelVipTableViewCell *)cell;
        ex_cell.dataSource=cm.userInfo;

    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

-(void)goToAllLevelVipViewController{
    
    MCAllLevelVipViewController * vc=[[MCAllLevelVipViewController alloc]init];
    self.navigationController.navigationBarHidden=NO;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}

/*
 * 跳转界面
 */
-(void)goToVCWithType:(NSInteger )type{
    if (type==1001) {
        self.navigationController.navigationBarHidden=NO;
        [self.navigationController popViewControllerAnimated:YES];
    }
}
@end












































